//
//  DataBase.m
//  Objc进阶Demo
//
//  Created by Don't update on 2018/7/27.
//  Copyright © 2018年 Don't update. All rights reserved.
//

#import "DataBase.h"

static DataBase *db;// 全集静态变量
static NSString *dbName;
@implementation DataBase

+(DataBase *)defaultDB{
    if (!db) {
        db = [[DataBase alloc]init];
    }
    return db;
}

-(instancetype)init{
    if (self = [super init]) {
        [self openDB:@"testDB"];
    }
    return self;
}


-(BOOL)openDB:(NSString *)sqlName{
    //获取本地数据库路径 一般写在对应程序的沙盒路径
    NSString *appendName = [NSString stringWithFormat:@"Library/Caches/%@.sqlite",sqlName];
    NSString *sqlPath = [NSHomeDirectory() stringByAppendingPathComponent:appendName];
    NSLog(@"path:/%@",sqlPath);
    int result = sqlite3_open([sqlPath UTF8String], &sqlite);
    if (result == SQLITE_OK) {
        dbName = sqlName;
        return YES;
    }
    return NO;
}

-(BOOL)deleteTableWithName:(NSString *)tableName{
    char *err;
    NSString *sql = [NSString stringWithFormat:@"DROP TABLE %@",tableName];
    int result = sqlite3_exec(sqlite, [sql UTF8String], NULL, NULL, &err);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
        return YES;
    }else{
        NSLog(@"FAIL:%s",err);
    }
    return NO;
}


-(BOOL)closeDB{
    int result = sqlite3_close(sqlite);
    if(result == SQLITE_OK){
        return  YES;
    }
    return NO;
}

-(BOOL)createTable:(NSString *)tableName{
    char *err;
    NSString *sql;
    sql = [NSString stringWithFormat:@"create table %@ ( page,type,message,create_time )",tableName];
    
    NSLog(@"[create SQL]: %@",sql);
    int result = sqlite3_exec(sqlite, [sql UTF8String], NULL, NULL, &err);
    
    if (result == SQLITE_OK) {
        NSLog(@"[Create SQL]: %@",sql);
        return YES;
    }else{
        NSLog(@"[Create SQL]: Fail");
        NSLog(@"error = %s",err);
    }
    return NO;
}

-(BOOL)InsertDataWithTabel:(NSString *)tableName
                WithColums:(NSArray *)ColumsArr
                  WithData:(NSArray *)dataArr{
    
    NSString *columsStr = ColumsArr[0];
    NSString *dataStr = dataArr[0];
    for (NSInteger i = 1; i<ColumsArr.count; i++) {
       columsStr =  [NSString stringWithFormat:@"%@,%@",columsStr,(NSString *)ColumsArr[i]];
       dataStr =  [NSString stringWithFormat:@"%@,%@",dataStr,(NSString *)dataArr[i]];
    }
    char *err;
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO %@ (%@) VALUES (%@)",tableName,columsStr,dataStr];
    NSLog(@"%@",sql);
    int result = sqlite3_exec(sqlite, [sql UTF8String],NULL , NULL, &err);
    if(result == SQLITE_OK){
        NSLog(@"插入成功");
        return  YES;
    }else{
        NSLog(@"Fail: %s",err);
    }
    return  NO;
}


-(void)selectTableName:(NSString *)tableName{
    NSString *sql =[NSString stringWithFormat:@"SELECT * FROM %@",tableName];
    sqlite3_stmt *stmt;
    NSMutableArray *selectDict = [[NSMutableArray alloc]init];
    //做查询前 检查sqlite语句是否正确
    int result = sqlite3_prepare_v2(sqlite, sql.UTF8String, -1, &stmt, NULL);//-1表示自动计算语句长度
    if (result == SQLITE_OK) {
        
        //提取查询的数据到stmt 一次提取一条
        //如果返回值SQLITE_ROW,就代表提取到了一条
        while (sqlite3_step(stmt)==SQLITE_ROW) {
            //将数据解析
            //获取表中字段个数
            NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
            int columCount = sqlite3_column_count(stmt);
            
            for (int i = 0 ;i<columCount; i++) {
               NSString *key =[[NSString alloc]initWithUTF8String:sqlite3_column_name(stmt, i)];
                
                NSString *value =[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(stmt, i)];
             
                [dict setObject:value forKey:key];
                [selectDict addObject:dict];
            }
            
        }
    }
    NSLog(@"%@",selectDict);
}








@end
